using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Agent.Dto;
using JTZY.Model.Agent;
using JTZY.Repository;
using JTZY.Service.Agent.IAgentService;
using JTZY.Model.Business;
using JTZY.Model.Order;

namespace JTZY.Service.Agent
{
    /// <summary>
    /// 代理商订单记录表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmsagentorderService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmsagentorderService : BaseService<Corecmsagentorder>, ICorecmsagentorderService
    {
        /// <summary>
        /// 查询代理商订单记录表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsagentorderDto> GetList(CorecmsagentorderQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .LeftJoin<Corecmsuser>((it, custer) => it.BuyUserId == custer.Id)
                .Select((it, custer) => new Corecmsagentorder
                {
                    Id=it.Id,
                    OrderId = it.OrderId,
                    Amount = it.Amount,
                    UserId = it.UserId,
                    UserName = custer.UserName,
                    CreateTime = it.CreateTime,
                    UpdateTime=it.UpdateTime
                })
                .Includes(x => x.CorecmsagentorderdetailsNav) //填充子对象
                .Where(predicate.ToExpression()).OrderByDescending(it => new { it.CreateTime })
                .ToPage<Corecmsagentorder, CorecmsagentorderDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Corecmsagentorder GetInfo(int Id)
        {
            var response = Queryable()
                .Includes(x => x.CorecmsagentorderdetailsNav) //填充子对象
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加代理商订单记录表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmsagentorder AddCorecmsagentorder(Corecmsagentorder model)
        {
            return Context.InsertNav(model).Include(s1 => s1.CorecmsagentorderdetailsNav).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改代理商订单记录表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCorecmsagentorder(Corecmsagentorder model)
        {
            return Context.UpdateNav(model).Include(z1 => z1.CorecmsagentorderdetailsNav).ExecuteCommand() ? 1 : 0;
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmsagentorder> QueryExp(CorecmsagentorderQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsagentorder>();
            predicate.And(it => it.UserId == parm.UserId);
            predicate.And(it => it.IsSettlement == 1);
            predicate = predicate.AndIF(parm.BeginCreateTime == null, it => it.CreateTime >= DateTime.Now.AddDays(-30).ToShortDateString().ParseToDateTime());
            predicate = predicate.AndIF(parm.BeginCreateTime != null, it => it.CreateTime >= parm.BeginCreateTime);
            predicate = predicate.AndIF(parm.EndCreateTime != null, it => it.CreateTime <= parm.EndCreateTime);

            return predicate;
        }
    }
}